#!/usr/bin/env python3
#-*- coding:utf-8 -*-
# 获取文件MD5值
import os
import hashlib
import datetime
import zlib
import stat



# 计算crc32
def GETCRC32(filePath):  
    if os.path.isfile(filePath):
        crc32_value = 0
        f=open(filePath, 'rb')
        for line in f:
            crc32_value = zlib.crc32(line, crc32_value)
        f.close
        return crc32_value & 0xFFFFFFFF  # 将结果限制为32位无符号整数
    else:
        return "未获取到crc32值"
    
def GetFileHashLib(filePath,hash_algorithm):
    hash_object = hashlib.new(hash_algorithm)
    if os.path.isfile(filePath):
        f=open(filePath, 'rb')
        for line in f:
            hash_object.update(line)
        f.close
    else:
        hash_object.update(filePath)
    return hash_object.hexdigest().upper()

#获取文件的 MD5、sha256、sha1 值
def GetFileHash(filePath):
    sha256_hash = GetFileHashLib(filePath, "sha256")
    md5_hash = GetFileHashLib(filePath, "md5")
    sha1_hash = GetFileHashLib(filePath, "sha1")
    CRC32=GETCRC32(filePath)
    return sha256_hash, md5_hash, sha1_hash, CRC32


# 字节转换
def CONVERTSIZE(size_bytes):
    # 单位后缀
    units = ['B', 'KB', 'MB', 'GB', 'TB']
    # 初始化单位索引
    unit_index = 0
    # 文件大小循环右移10位，直到小于1024，即1KB
    while size_bytes >= 1024 and unit_index < len(units) - 1:
        size_bytes /= 1024.0
        unit_index += 1
    return "{:.2f} {}".format(size_bytes, units[unit_index])

#获取文件大小
def GetFileSize(filePath):
    SIZEBYTE=os.path.getsize(filePath)
    SIZEUNIT=CONVERTSIZE(SIZEBYTE)
    return SIZEUNIT,SIZEBYTE

#获取文件最后修改时间
def GetFileModifiedTime(filePath):
    mtime = os.path.getmtime(filePath)
    last_modified_time = datetime.datetime.fromtimestamp(mtime)
    return last_modified_time

# 获取文件权限
def GETFILEPERmissions(file_path):
    # 获取文件状态信息
    file_stat = os.stat(file_path)
    # 提取文件权限
    permissions = {
        "owner(用户)": {
            "readable(读)": bool(file_stat.st_mode & stat.S_IRUSR),
            "writable(写)": bool(file_stat.st_mode & stat.S_IWUSR),
            "executable(执行)": bool(file_stat.st_mode & stat.S_IXUSR)
        },
        "group(组)": {
            "readable(读)": bool(file_stat.st_mode & stat.S_IRGRP),
            "writable(写)": bool(file_stat.st_mode & stat.S_IWGRP),
            "executable(执行)": bool(file_stat.st_mode & stat.S_IXGRP)
        },
        "others(访问)": {
            "readable(读)": bool(file_stat.st_mode & stat.S_IROTH),
            "writable(写)": bool(file_stat.st_mode & stat.S_IWOTH),
            "executable(执行)": bool(file_stat.st_mode & stat.S_IXOTH)
        }
    }

    return permissions

# 获取文件名称
def GetFileName(filePath):
    # 使用 os.path.basename() 函数获取文件名部分
    filename = os.path.basename(filePath)
    return filename

def Main():
    ArgOfFilePath=__import__("sys").argv[1]

    if os.path.isfile(ArgOfFilePath):
        FILEINFO=os.stat(ArgOfFilePath)
        FILENAME=GetFileName(ArgOfFilePath)
        MODTIME=GetFileModifiedTime(ArgOfFilePath)
        SIZEUNIT,SIZEBYTE=GetFileSize(ArgOfFilePath)
        SHA256, HASHMD5, SHA1, CRC32=GetFileHash(ArgOfFilePath)
        CREATETIME=datetime.datetime.fromtimestamp(FILEINFO.st_ctime)
        FWTIME=datetime.datetime.fromtimestamp(FILEINFO.st_atime)
        permissions=GETFILEPERmissions(ArgOfFilePath)

        print("------------->>>>文件信息<<<<-------------")
        print(f"文件路径: {ArgOfFilePath}")
        print(f"文件名:《{FILENAME}》")
        print(f"文件创建时间: {CREATETIME}")
        print(f"文件修改时间: {MODTIME}")
        print(f"文件访问时间: {FWTIME}")
        print(f"文件大小: {SIZEUNIT}    字节数: ({SIZEBYTE})")
        print(f"文件CRC32值: {CRC32}")
        print(f"文件SHA256值: {SHA256}")
        print(f"文件MD5值: {HASHMD5}")
        print(f"文件SHA1值: {SHA1}")
        print(f"文件MD5值的第12-18位: {HASHMD5[12:18]}")

        print("文件权限:")
        for category, perms in permissions.items():
            print(f"    {category}:")
            for perm_type, value in perms.items():
                print(f"        {perm_type}: {'Yes' if value else 'No'}")
    else:
        print("当前路径不是一个文件路径")



Main()